//
//  WXPProductPackageTopSliderMoneyView.m
//  MexicoLoan
//
//  Created by WXP on 2025/3/13.
//
//

#import "WXPProductPackageTopSliderMoneyView.h"

@interface WXPProductPackageTopSliderMoneyView ()



@end

@implementation WXPProductPackageTopSliderMoneyView

#pragma mark - lazy
- (UILabel *)labelTitle{
    if (_labelTitle == nil) {
        _labelTitle = [UILabel  new];
        _labelTitle.textColor = COLOR_BLACK;
        _labelTitle.fontNum = 16;
        [_labelTitle fitTitle:@"Cuota máxima($)" variable:0];
    }
    return  _labelTitle;
}
- (UILabel *)labelMoney{
    if (_labelMoney == nil) {
        _labelMoney = [UILabel  new];
        _labelMoney.textColor = COLOR_BLACK;
        _labelMoney.font = [UIFont boldSystemFontOfSize:36];
    }
    return  _labelMoney;
}
- (UISlider *)customSlider{
    if (_customSlider == nil) {
        _customSlider = [UISlider  new];
        _customSlider.widthHeight = XY(SCREEN_WIDTH-30-30, 25);
        // [_customSlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents: UIControlEventValueChanged];
        // 修改后的绑定（结束滑动时触发）
        [_customSlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
        _customSlider.minimumValue = 0.0;
        _customSlider.maximumValue = 100.0;
        _customSlider.value = 100.0;
        [_customSlider setThumbImage:[UIImage imageNamed:@"icon-slider"] forState:UIControlStateNormal];
        [_customSlider setMinimumTrackTintColor:COLOR_WHITE];
        [_customSlider setMaximumTrackTintColor:[UIColor colorWithHexString:@"929292" alpha:0.3]];
        [GlobalMethod setRoundView:_customSlider color:[UIColor clearColor] numRound:6/2 width:0];
    }
    return  _customSlider;
}
- (UILabel *)labelMinMoney{
    if (_labelMinMoney == nil) {
        _labelMinMoney = [UILabel  new];
        _labelMinMoney.textColor = COLOR_BLACK;
        _labelMinMoney.font = [UIFont boldSystemFontOfSize:16];
    }
    return  _labelMinMoney;
}
- (UILabel *)labelMaxMoney{
    if (_labelMaxMoney == nil) {
        _labelMaxMoney = [UILabel  new];
        _labelMaxMoney.textColor = COLOR_BLACK;
        _labelMaxMoney.font = [UIFont boldSystemFontOfSize:16];
    }
    return  _labelMaxMoney;
}
- (UILabel *)labelSubTitle{
    if (_labelSubTitle == nil) {
        _labelSubTitle = [UILabel  new];
        _labelSubTitle.textColor = COLOR_BLACK;
        _labelSubTitle.font = [UIFont systemFontOfSize:12];
        [_labelSubTitle fitTitle:@"Opcional" variable:0];
    }
    return  _labelSubTitle;
}
- (UILabel *)labelTime
{
    if (_labelTime == nil) {
        _labelTime = [UILabel  new];
        _labelTime.textColor = COLOR_WHITE;
        _labelTime.fontNum = 14;
        [_labelTime fitTitle:@"01:00" variable:0];
    }
    return  _labelTime;
}
- (UIButton *)btnSubmit{
    if (_btnSubmit == nil) {
        _btnSubmit = [UIButton  new];
        _btnSubmit.widthHeight = XY(SCREEN_WIDTH-LEFT_MARDIN*2, BUTTON_HEIGHT);
        [GlobalMethod setRoundView:_btnSubmit color:[UIColor clearColor] numRound:BUTTON_HEIGHT/2 width:0];
        [_btnSubmit setTitleColor:COLOR_WHITE forState:UIControlStateNormal];
        _btnSubmit.titleLabel.fontNum = 14;
        [_btnSubmit setTitle:@"Solicitar ahora" forState:UIControlStateNormal];
        _btnSubmit.backgroundColor = COLOR_BLACK;
        _btnSubmit.tag = 1;
        [_btnSubmit addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _btnSubmit;
}
- (UIView *)viewMoneyBack{
    if (_viewMoneyBack == nil) {
        _viewMoneyBack = [UIView  new];
        _viewMoneyBack.backgroundColor = COLOR_WHITE;
        [GlobalMethod setRoundView:_viewMoneyBack color:[UIColor clearColor] numRound:16 width:0];
        _viewMoneyBack.width = SCREEN_WIDTH-LEFT_MARDIN*2;
    }
    return  _viewMoneyBack;
}
- (UILabel *)labelLeftMoneyTitle{
    if (_labelLeftMoneyTitle == nil) {
        _labelLeftMoneyTitle = [UILabel  new];
        _labelLeftMoneyTitle.textColor = COLOR_BLACK;
        _labelLeftMoneyTitle.fontNum = 12;
        [_labelLeftMoneyTitle fitTitle:@"Cuota utilizada($)" variable:0];
    }
    return  _labelLeftMoneyTitle;
}
- (UILabel *)labelLeftMoney{
    if (_labelLeftMoney == nil) {
        _labelLeftMoney = [UILabel  new];
        _labelLeftMoney.textColor = COLOR_BLACK;
        _labelLeftMoney.font = [UIFont boldSystemFontOfSize:20];
    }
    return  _labelLeftMoney;
}
- (UILabel *)labelRightMoneyTitle{
    if (_labelRightMoneyTitle == nil) {
        _labelRightMoneyTitle = [UILabel  new];
        _labelRightMoneyTitle.textColor = COLOR_BLACK;
        _labelRightMoneyTitle.fontNum = 12;
        [_labelRightMoneyTitle fitTitle:@"Cuota restante($)" variable:0];
    }
    return  _labelRightMoneyTitle;
}
- (UILabel *)labelRightMoney{
    if (_labelRightMoney == nil) {
        _labelRightMoney = [UILabel  new];
        _labelRightMoney.textColor = COLOR_BLACK;
        _labelRightMoney.font = [UIFont boldSystemFontOfSize:20];
    }
    return  _labelRightMoney;
}
- (UIView *)lineView{
    if (_lineView == nil) {
        _lineView = [UIView  new];
        _lineView.width = 1;
        _lineView.backgroundColor = COLOR_LINE;
    }
    return  _lineView;
}
- (UIView *)viewSection{
    if (_viewSection == nil) {
        _viewSection = [UIView  new];
        _viewSection.widthHeight = XY(SCREEN_WIDTH-LEFT_MARDIN*2, 48);
        _viewSection.backgroundColor = COLOR_WHITE;
        UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:_viewSection.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(16, 16)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _viewSection.bounds;
        maskLayer.path = maskPath.CGPath;
        _viewSection.layer.mask = maskLayer;
    }
    return  _viewSection;
}
- (UILabel *)labelSectionTitle{
    if (_labelSectionTitle == nil) {
        _labelSectionTitle = [UILabel  new];
        _labelSectionTitle.textColor = COLOR_BLACK;
        _labelSectionTitle.font = [UIFont boldSystemFontOfSize:16];
        [_labelSectionTitle fitTitle:@"Mi cartera de productos" variable:0];
    }
    return  _labelSectionTitle;
}
- (UIView *)lineBottom
{
    if (_lineBottom == nil) {
        _lineBottom = [UIView  new];
        _lineBottom.backgroundColor = COLOR_LINE;
        _lineBottom.height = 1;
    }
    return  _lineBottom;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.width = SCREEN_WIDTH;
        [self addSubView];
    }
    return self;
}

//添加subview
- (void)addSubView{
    [self addSubview:self.labelTitle];
    [self addSubview:self.labelMoney];
    [self addSubview:self.customSlider];
    [self addSubview:self.labelMinMoney];
    [self addSubview:self.labelMaxMoney];
    [self addSubview:self.labelSubTitle];
    [self addSubview:self.btnSubmit];
    [self.btnSubmit addSubview:self.labelTime];
    [self addSubview:self.viewMoneyBack];
    [self.viewMoneyBack addSubview:self.labelLeftMoneyTitle];
    [self.viewMoneyBack addSubview:self.labelLeftMoney];
    [self.viewMoneyBack addSubview:self.labelRightMoneyTitle];
    [self.viewMoneyBack addSubview:self.labelRightMoney];
    [self.viewMoneyBack addSubview:self.lineView];
    [self addSubview:self.viewSection];
    [self.viewSection addSubview:self.labelSectionTitle];
    [self.viewSection addSubview:self.lineBottom];
    //初始化页面
    [self resetMoney:nil];
}

- (void)sliderValueChanged:(UISlider *)sender {
    int value = [[NSString stringWithFormat:@"%.00f", sender.value] intValue];
    NSLog(@" --- value --- %d", value);
    if(self.blockSliderValueChanged){
        self.blockSliderValueChanged(value);
    }
}

// 开始倒计时
- (void)startCountdownTime{
    if (self.timer) {
        dispatch_source_cancel(self.timer);
        self.timer = nil;
    }
    //倒计时时间
    __block NSInteger timeOut = 60;
    //建一个全局队列，默认优先级
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    //每秒执行一次
    dispatch_source_set_timer(self.timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(self.timer, ^{
        
        //倒计时结束，关闭
        if (timeOut <= 0) {
            dispatch_source_cancel(self.timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self endCountdownTime];
            });
        } else {
            int seconds = timeOut % 60;
            NSInteger firstNum = timeOut/ 60;
            NSString * firstStr = firstNum>0?@"01":@"00";
            
            NSString *timeStr = [NSString stringWithFormat:@"%0.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.labelTime fitTitle:[NSString stringWithFormat:@"%@:%@",firstStr,timeStr] variable:0];
                self.labelTime.leftCenterY = XY(30, self.btnSubmit.height/2);
            });
            timeOut--;
        }
    });
    dispatch_resume(self.timer);
}

// 倒计时结束
- (void)endCountdownTime{
    if (self.timer) {
        dispatch_source_cancel(self.timer);
        self.timer = nil;
    }
    self.labelTime.hidden = true;
}

#pragma mark - 点击事件
- (void)btnClick:(UIButton *)sender{
    [NSObject clickFeedback];
    [sender addClickAnimate];
    switch (sender.tag) {
        case 1:
        {
            if(self.blockApplyClick){
                self.blockApplyClick();
            }
        }
            break;
        default:
            break;
    }
}

#pragma mark - 刷新view
- (void)resetMoney:(NSDictionary *)dic{
    NSString * unused = strDot2F([dic doubleValueForKey:KEY_unused]);
    NSString * used = strDot2F([dic doubleValueForKey:KEY_used]);
    
    self.labelTitle.leftTop = XY(30, 40);
    [self.labelMoney fitTitle:[GlobalMethod showMoney:unused] variable:0];
    self.labelMoney.leftCenterY = XY(self.labelTitle.right+20, self.labelTitle.centerY);
    self.customSlider.centerXTop = XY(SCREEN_WIDTH/2, self.labelTitle.bottom+self.labelTitle.top);
    
    
    [self.labelMinMoney fitTitle:[NSString stringWithFormat:@"$ %@",[GlobalMethod showMoney:@"0"]] variable:0];
    self.labelMinMoney.leftTop = XY(self.labelTitle.left, self.customSlider.bottom+6);
    
    [self.labelMaxMoney fitTitle:[NSString stringWithFormat:@"$ %@",[GlobalMethod showMoney:unused]] variable:0];
    self.labelMaxMoney.rightCenterY = XY(SCREEN_WIDTH-self.labelTitle.left, self.labelMinMoney.centerY);
    
    self.labelSubTitle.centerXCenterY = XY(SCREEN_WIDTH/2, self.labelMinMoney.centerY);
    
    self.btnSubmit.centerXTop = XY(SCREEN_WIDTH/2, self.labelMinMoney.bottom+20);
    
    self.labelLeftMoneyTitle.centerXTop = XY(self.viewMoneyBack.width/4, 26);
    [self.labelLeftMoney fitTitle:[GlobalMethod showMoney:used] variable:0];
    self.labelLeftMoney.leftTop = XY(self.labelLeftMoneyTitle.left, self.labelLeftMoneyTitle.bottom+7);
    
    self.labelRightMoneyTitle.centerXCenterY = XY(self.viewMoneyBack.width/4*3, self.labelLeftMoneyTitle.centerY);
    [self.labelRightMoney fitTitle:[GlobalMethod showMoney:unused] variable:0];
    self.labelRightMoney.rightCenterY = XY(self.labelRightMoneyTitle.right, self.labelLeftMoney.centerY);
    
    self.viewMoneyBack.height = self.labelLeftMoney.bottom+self.labelLeftMoneyTitle.top;
    self.viewMoneyBack.centerXTop = XY(self.width/2, self.btnSubmit.bottom+22);
    self.lineView.height = self.viewMoneyBack.height-29-29;
    self.lineView.centerXCenterY = XY(self.viewMoneyBack.width/2, self.viewMoneyBack.height/2);
    
    self.viewSection.centerXTop = XY(self.width/2, self.viewMoneyBack.bottom+LEFT_MARDIN);
    self.lineBottom.widthHeight = XY(self.viewSection.width-LEFT_MARDIN*2, 1);
    self.lineBottom.centerXBottom = XY(self.viewSection.width/2, self.viewSection.height);
    self.labelSectionTitle.leftCenterY = XY(LEFT_MARDIN, (self.viewSection.height)/2);
    
    self.height = self.viewSection.bottom;
}



@end
